package jvlite.ioc;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.Callable;

import jvlite.core.JLiteContext;
import jvlite.core.PluginManger;
import jvlite.mvc.annotaion.Action;
import jvlite.mvc.annotaion.ActionMetod;
import jvlite.mvc.annotaion.Boot;
import jvlite.mvc.annotaion.Insert;
import jvlite.mvc.annotaion.Part;
import jvlite.mvc.annotaion.PointCut;
import jvlite.plugin.IPlugin;

public class BeanGenerater implements Callable<Boolean> {
	
	public static void mkAnnos(){
		ArrayList<Class<? extends Annotation>> allAnnotationTypes = JLiteContext.allAnnotationTypes;
		allAnnotationTypes.add(Insert.class);
		allAnnotationTypes.add(Action.class);
		allAnnotationTypes.add(ActionMetod.class);
		allAnnotationTypes.add(PointCut.class);
		allAnnotationTypes.add(Part.class);
		allAnnotationTypes.add(Boot.class);
	}
	
	public static void mkMaps() {
		for (Map.Entry<String, Object> entry : BeanFactory.getBeanMap().entrySet()) {
			Class<? extends Object> cls = entry.getValue().getClass();
			for ( Class<? extends Annotation> clazz : JLiteContext.allAnnotationTypes) {
				if (cls.isAnnotationPresent(clazz)) {
					BeanFactory.getDIMap().put(entry.getKey(), entry.getValue());
				}
			}
			// fill plugin set
			if (entry.getValue() instanceof IPlugin) {
				PluginManger.pluginSet.add((IPlugin)entry.getValue());
			}
		}
	}

	@Override
	public Boolean call() throws Exception {
		mkAnnos();
		mkMaps();
		return true;
	}

}
